<%--
  Created by IntelliJ IDEA.
  User: lichengming
  Date: 2017/6/15
  Time: 下午12:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/resources.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>系统和模块配置</title>

    <link rel="stylesheet" href="/starCms/css/upload/webuploader.css">
    <link rel="stylesheet" href="/starCms/css/tree/bootstrap-treeview.min.css">
    <script src="/starCms/js/jquery.md5.js"></script>
    <script src="/starCms/js/tree/bootstrap-treeview.min.js"></script>

    <style type="text/css">

        .ems_add_gbtn {
            flex: auto;
            display: flex;
            justify-content: flex-end;
        }

        .ems_add_heading {
            display: flex;
            flex-direction: row;
            align-items: center;
            padding: 5px 10px;
        }
        .ems_form_reset {
            margin-left: 10px;
        }

        .ems_form_checkbox_g>label {
               width: 120px;
               margin-right: 10px;
        }

        .ems_form_checkbox_g>label>a {
            display: none;
            text-decoration: none;
        }

        #add_new_role>a {
            text-decoration: none;
        }

        .well_close {
            position: absolute;
            top: 10px;
            right: 10px;
        }

        #paramsList .well {
            position: relative;
        }

        #returnParamList .well {
            position: relative;
        }
    </style>
</head>
<body>

<div class="ems_form_add">
    <div class="panel panel-default">
        <div class="panel-heading ems_add_heading">
            <h3 class="panel-title" style="color: inherit;">系统和模块添加</h3>

            <div class="ems_add_gbtn">
                <button type="button" class="btn btn-primary ems_form_submit_add">
                    <span class="glyphicon glyphicon-check"></span> 保存
                </button>
                <button type="button" class="btn btn-default ems_form_reset">
                    <span class="glyphicon glyphicon-shopping-cart"></span> 重置
                </button>
            </div>
        </div>

        <form class="form-horizontal ems_internal_form_add">
            <div class="form-group">
                <label class="col-sm-3 control-label">系统：</label>
                <div class="col-sm-6">
                    <input type="text" name="systemNameAdd" required id="systemNameAdd" class="form-control" placeholder="必填,不超过32">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">模块：</label>
                <div class="col-sm-6">
                    <input type="text" name="moduleNameAdd" required id="moduleNameAdd" class="form-control" placeholder="必填,不超过32">
                </div>
            </div>
        </form>
    </div>
</div>


<div class="ems_form">
    <div class="panel panel-default">
        <div class="panel-heading ems_add_heading">
            <h3 class="panel-title" style="color: inherit;">添加系统模块</h3>

            <div class="ems_add_gbtn">
                <button type="button" class="btn btn-primary ems_form_submit">
                    <span class="glyphicon glyphicon-check"></span> 保存
                </button>
                <button type="button" class="btn btn-default ems_form_reset">
                    <span class="glyphicon glyphicon-shopping-cart"></span> 重置
                </button>
            </div>
        </div>

        <form class="form-horizontal ems_internal_form">
            <div class="form-group">
                <label class="col-sm-3 control-label">系统：</label>
                <div class="col-sm-6">
                        <select id="systemId" class="form-control">
                            <option value="-1">请选择</option>
                            <c:forEach items="${systemList}" var="list" varStatus="status">
                                <option id="systemName" name="systemName" value="${list.id}">${list.system_name }</option>
                            </c:forEach>
                        </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">模块：</label>
                <div class="col-sm-6">
                    <input type="text" name="moduleName" required id="moduleName" class="form-control" placeholder="必填,不超过32">
                </div>
            </div>
        </form>
    </div>
</div>

<div class="ems_form" id="paramsList">
    <div class="panel panel-default">
        <div class="panel-heading ems_add_heading">
            <h3 class="panel-title" style="color: inherit;">修改系统名称和状态</h3>

            <div class="ems_add_gbtn">
                <button type="button" id="system_name_edit" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                    编辑
                </button>
                <button type="button" class="btn btn-default ems_form_reset">
                    <span class="glyphicon glyphicon-shopping-cart"></span> 重置
                </button>
            </div>
        </div>

        <form class="form-horizontal ems_internal_form">
            <div class="form-group">
                <label class="col-sm-3 control-label">系统：</label>
                <div class="col-sm-6">
                        <select id="getSystemNames" class="form-control">
                            <option value="-1">请选择</option>
                            <c:forEach items="${systemList}" var="list" varStatus="status">
                                <option id="systemNames" name="systemNames" value="${list.id}">${list.system_name }</option>
                            </c:forEach>
                        </select>
                    <%--<input type="text" name="moduleName" required id="getmoduleName" class="form-control" placeholder="必填,不超过32">--%>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="ems_table_content">
    <div class="panel panel-default">
        <div class="panel-heading">系统和模块列表</div>
        <div id="table_tools">
            <div class="form-inline" role="form">
                <button type="button" id="system_module_edit" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                    编辑
                </button>
                <button type="button" id="systemModule_delete" class="btn btn-outline btn-danger">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    删除
                </button>
                <button type="button" id="systemModule_stop" class="btn btn-warning">
                    <i class="glyphicon glyphicon-remove" aria-hidden="true"></i>
                    停用
                </button>
                <button type="button" id="systemModule_start" class="btn">
                    <i class="glyphicon glyphicon-ok" aria-hidden="true"></i>
                    启动
                </button>
            </div>
        </div>
        <table id="merchantTable"
               data-url="/starCms/configuration/pagination">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-field="system_name" >系统名称</th>
                <th data-field="module_name">模块名称</th>
                <th data-field="status" data-formatter="statusFormat">状态</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<script>
    function statusFormat(srcValue) {
        if (srcValue == 1) {
            return "<span style='color: #3cb371;font-weight: bold' >正常</span>";
        }else {
            return "<span style='color: red;font-weight: bold'>停用</span>";
        }
    }
</script>
<script>
    $(function () {

        $("#merchantTable").createTable();

        //窗体
        var dialogOption = {
            width:"500px",
            height:"450px",
            url:"/starCms/configuration/edit",
            yesFn:function(){
                var result = systemModuleSave();
                if (result) search('#merchantTable',getQueryParams());
                return result;
            }
        }
        //编辑
        $("#system_module_edit").on("click",function () {
            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要编辑的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据编辑");
                return;
            }

            dialogOption.title = "分类编辑";
            dialogOption.data = {id : selectRows[0].id};
            $("#system_param_add_dialog").openDialog(dialogOption);
        });
    })
</script>

<script>
    //删除
    $("#systemModule_delete").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要删除的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行删除");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var system_module_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{system_module_id:system_module_id},
            type:"POST",
            url:"/starCms/configuration/delete",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/configuration/sysAndMolAdd"
                    })
                }else {
                    $.errorAlert("删除失败");
                }
            }
        });
    });
</script>

<script>

    //废止
    $("#systemModule_stop").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要停止的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行停止");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var module_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{module_id:module_id},
            type:"POST",
            url:"/starCms/configuration/systemModuleStop",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/configuration/sysAndMolAdd"
                    })
                }else {
                    $.errorAlert("停止失败");
                }
            }
        });
    });
</script>

<script>

    //启动
    $("#systemModule_start").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要启动的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行启动");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var module_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{module_id:module_id},
            type:"POST",
            url:"/starCms/configuration/systemModuleStart",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/configuration/sysAndMolAdd"
                    })
                }else {
                    $.errorAlert("启动失败");
                }
            }
        });
    });
</script>

<script>

    var systemId;
    $("#systemId").change(function(){
        systemId = $(this).val();
    });

    $.validator.setDefaults({
        //认证成功
        submitHandler: function(form) {
            var data = {};
            data["module_name"] = $.trim($("#moduleName").val());
            data["systemId"] = systemId;
            saveInterface(data);
        }
    });

    function getSystemList() {
        var paramList = [];
        $("#paramsList .well").each(function (index,val) {
            var param = {};
            param['systemName'] = $(val).find("select option:selected").val();
            paramList.push(param);
        });
        return paramList;
    }

    function saveInterface(data) {
        $.emsAjax({
            url:"/starCms/configuration/sysAndMolAddSave",
            type:"POST",
            data:data,
            success:function (resp) {
                if (resp) {
                    $.alert("保存成功",null,null,function () {
                        location.reload();
                    })
                }else {
                    $.errorAlert("保存失败");
                }
            }
        })
    }

    //提交按钮
    $(".ems_form_submit").on("click",function () {
        var form = $(this).parents(".ems_form").find("form:first");
        form.submit();
    });

    //表单验证
    $(".ems_internal_form").validate();
</script>

<%--添加系统模块--%>
<script>

    var systemNameAdd = {};
    var moduleNameAdd = {};

    $.validator.setDefaults({
        //认证成功
        submitHandler: function(form) {
            var data = {};
            debugger;
            data["systemName"] = systemNameAdd;
            data["moduleName"] = moduleNameAdd;
            saveInterface(data);
        }
    });

    function saveInterface(data) {
        $.emsAjax({
            url:"/starCms/configuration/newSysAndMolAddSave",
            type:"POST",
            data:data,
            success:function (resp) {
                if (resp) {
                    $.alert("保存成功",null,null,function () {
                        location.reload();
                    })
                }else {
                    $.errorAlert("保存失败");
                }
            }
        })
    }

    //提交按钮
    $(".ems_form_submit_add").on("click",function () {
        systemNameAdd = $("#systemNameAdd").val();
        moduleNameAdd = $("#moduleNameAdd").val();
        var form = $(this).parents(".ems_form_add").find("form:first");
        form.submit();
    });

    //表单验证
    $(".ems_internal_form_add").validate();
</script>


<script>

        $(function () {

            var ststemId = {};

            $("#getSystemNames").change(function(){
                systemId = $(this).val();
            });

            //窗体
            var dialogOption = {
                width:"500px",
                height:"450px",
                url:"/starCms/configuration/systemNameEdit",
                yesFn:function(){
                    var result = systemNameEdit();
                    return result;
                }
            }
            //修改系统名称提交按钮
            $("#system_name_edit").on("click",function () {
                dialogOption.title = "系统名称和状态编辑";
                dialogOption.data = {systemId:systemId};
                $("#system_edit_dialog").openDialog(dialogOption);
            });
        });

</script>


<div id="system_param_add_dialog"></div>
<div id="system_edit_dialog"></div>
</body>
</html>
